<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="账户id" prop="advertiserId">
        <el-input
          v-model="queryParams.advertiserId"
          placeholder="请输入账户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应用" prop="pkg">
        <el-select v-model="queryParams.pkg" placeholder="应用" filterable clearable style="width: 100%">
          <el-option
            v-for="item in productList"
            :key="item.value"
            :label="item.name"
            :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="渠道" prop="channel">
        <el-select v-model="queryParams.channel" placeholder="渠道" filterable clearable style="width: 100%">
          <el-option
          v-for="item in dict.type.sys_channel"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['launch:income:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dauList">
      <el-table-column label="日期" align="center" prop="date" />
      <el-table-column label="渠道" align="center" prop="channel" />
      <el-table-column label="应用" align="center" prop="appName"/>
      <el-table-column label="账户" align="center" prop="advertiserId" width="200"/>
      <el-table-column label="DAU" align="center" prop="dau" />
      <el-table-column label="新增用户数" align="center" prop="saveNum" />
      <el-table-column label="新增收益" align="center" prop="saveIncome" />
      <el-table-column label="新增ipu" align="center" prop="saveIpu" />
      <el-table-column label="新增ecpm" align="center" prop="saveEcpm" />
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { queryDauList, exportDauList } from "@/api/business/income/income";
import { getAllAppList } from '@/api/configs/appConfig';

export default {
  name: "dau",
  dicts: ['sys_channel'],
  data() {
    return {
      // 日期范围
      dateRange: [new Date(),new Date()],
      productList:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      dauList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        channel: null,
        pkg: null,
      },
    };
  },
  created() {
    this.getAppList();
    this.getList();
  },
  methods: {
    formatDate(date) {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}${month}${day}`;
    },
    //查询应用列表
    getAppList(){
      getAllAppList().then(response => {
        this.productList = response.data.map(item => {
          return {
            id: item.id,
            appcode: item.appcode,
            name: item.appname,
            value: item.apppkg
          }
        })
      })
    },
    getList() {
      this.loading = true;
      console.log(this.dateRange)
      if(this.dateRange){
        this.queryParams.beginDate = this.formatDate(this.dateRange[0]);
        this.queryParams.endDate = this.formatDate(this.dateRange[1]);
      }
      console.log(this.queryParams)
      queryDauList(this.queryParams).then(response => {
        this.dauList = response.rows;
        this.dauList.forEach(user=>{
          this.productList.forEach(product=>{
            if(user.pkg===product.value){
              user.appName = product.name;
            }
          })
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [new Date(),new Date()];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有数据项？').then(() => {
        this.exportLoading = true;
        return exportDauList(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
